home *** CD-ROM | disk | FTP | other *** search
/ PC World 2007 December / PCWorld_2007-12_cd.bin / domacnost a kancelar / autoit / autoit-v3-setup.exe / Include / String.au3 < prev    next >
Encoding:
Text File  |  2007-09-08  |  17.5 KB  |  454 lines

  1. #include-once
  2.  
  3. ;===============================================================================
  4. ;
  5. ; Function Name:    _HexToStr("hex")
  6. ; Description:      Convert a hex string of characters to ASCII Characters.
  7. ; Parameter(s):     $strHex is the hex string you want to convert.
  8. ; Requirement(s):   Hex Input.
  9. ; Return Value(s):  On Success - Returns the converted string of characters.
  10. ;                   On Failure - -1  and sets @ERROR = 1
  11. ; Author(s):        Jarvis Stubblefield
  12. ; Corrected:        2005/09/04 jpm error checking
  13. ;
  14. ;===============================================================================
  15.  
  16. Func _HexToString($strHex)
  17.     Local $strChar, $aryHex, $i, $iDec, $Char, $iOne, $iTwo
  18.     
  19.     $aryHex = StringSplit($strHex, "")
  20.     If Mod($aryHex[0], 2) <> 0 Then
  21.         SetError(1)
  22.         Return -1
  23.     EndIf
  24.     
  25.     For $i = 1 To $aryHex[0]
  26.         $iOne = $aryHex[$i]
  27.         $i = $i + 1
  28.         $iTwo = $aryHex[$i]
  29.         $iDec = Dec($iOne & $iTwo)
  30.         If @error <> 0 Then
  31.             SetError(1)
  32.             Return -1
  33.         EndIf
  34.         
  35.         $Char = Chr($iDec)
  36.         $strChar &= $Char
  37.     Next
  38.     
  39.     Return $strChar
  40. EndFunc   ;==>_HexToString
  41.  
  42. ;==================================================================================================
  43. ; Function Name:         _StringAddComma($sStr, $sSeparator, $sEnd)
  44. ;
  45. ; Parameters:            $sStr:   The Numbered string to pass
  46. ;                        $sSeparator:   The seperator string you wish to use as the delimiter
  47. ;                        $sEnd:   The End delimiter to distinguish decimal remainder
  48. ;
  49. ; Description:           Adds your preferred delimiter to a Number string
  50. ;
  51. ; Return Value(s)
  52. ;                        On Success:        @Error: = 0 No Error | Returns the number string with its separators
  53. ;                        On Failure:        @Error = 1: String passed was not a number
  54. ; Author(s):             SmOke_N
  55. ;==================================================================================================
  56. Func _StringAddComma($sStr, $sSeparator = -1, $sEnd = -1)
  57.     If $sSeparator = -1 Or $sSeparator = Default Then $sSeparator = ','
  58.     If $sEnd = -1 Or $sEnd = Default Then $sEnd = '.'
  59.     Local $aNum = StringSplit($sStr, $sEnd), $sHold = '', $aSRE, $bUB = False
  60.     If UBound($aNum) > 2 Then
  61.         $aSRE = StringRegExp($aNum[1], '(\d+)(\d{3})', 1)
  62.         $bUB = True
  63.     Else
  64.         $aSRE = StringRegExp($sStr, '(\d+)(\d{3})', 3)
  65.     EndIf
  66.     If UBound($aSRE) = 2 Then
  67.         While IsArray($aSRE)
  68.             $sHold = $sSeparator & $aSRE[1] & $sHold
  69.             $aSRE = StringRegExp($aSRE[0], '(\d+)(\d{3})', 3)
  70.         WEnd
  71.     EndIf
  72.     Local $nStrLen = StringLen(StringReplace($sHold, $sSeparator, ''))
  73.     If $bUB And $sHold Then
  74.         Return StringTrimRight($aNum[1], $nStrLen) & $sHold & $sEnd & $aNum[2]
  75.     ElseIf $sHold Then
  76.         Return StringTrimRight($sStr, $nStrLen) & $sHold
  77.     EndIf
  78.     Return SetError(1, 0, $sStr)
  79. EndFunc   ;==>_StringAddComma
  80.  
  81. ;==================================================================================================
  82. ; Function Name:        _StringBetween($sString, $sStart, $sEnd, $vCase, $iSRE)
  83. ;
  84. ; Parameters:           $sString:     The string to search
  85. ;                       $sStart:      The beginning of the string to find
  86. ;                       $sEnd:        The end of the string to find
  87. ;                       $vCase:       Case sensitive search:  Default or -1 = Not case sensitive
  88. ;                       $iSRE:        Choose whether to use StringRegExp or Regular Sting Manipulation to get the result
  89. ;                                     Default or -1:  Regular String Manipulation used (Non StringRegExp())
  90. ;
  91. ; Description:          Returns the string between the start search ($sStart) and the end search ($sEnd)
  92. ;
  93. ; Requirement(s)        AuotIt Beta 3.2.1.8 or higher
  94. ;
  95. ; Return Value(s)       On Success:    A 0 based array [0] contains the first found string
  96. ;                       On Failure:    @Error = 1: No inbetween string was found
  97. ;
  98. ; Author(s):            SmOke_N
  99. ;                       Thanks to Valik for helping with the new StringRegExp (?s)(?i) isssue
  100. ;==================================================================================================
  101.  
  102. Func _StringBetween($sString, $sStart, $sEnd, $vCase = -1, $iSRE = -1)
  103.     If $iSRE = -1 Or $iSRE = Default Then
  104.         If $vCase = -1 Or $vCase = Default Then
  105.             $vCase = 0
  106.         Else
  107.             $vCase = 1
  108.         EndIf
  109.         Local $sHold = '', $sSnSStart = '', $sSnSEnd = ''
  110.         While StringLen($sString) > 0
  111.             $sSnSStart = StringInStr($sString, $sStart, $vCase)
  112.             If Not $sSnSStart Then ExitLoop
  113.             $sString = StringTrimLeft($sString, ($sSnSStart + StringLen($sStart)) - 1)
  114.             $sSnSEnd = StringInStr($sString, $sEnd, $vCase)
  115.             If Not $sSnSEnd Then ExitLoop
  116.             $sHold &= StringLeft($sString, $sSnSEnd - 1) & Chr(1)
  117.             $sString = StringTrimLeft($sString, $sSnSEnd)
  118.         WEnd
  119.         If Not $sHold Then Return SetError(1, 0, 0)
  120.         $sHold = StringSplit(StringTrimRight($sHold, 1), Chr(1))
  121.         Local $avArray[UBound($sHold) - 1]
  122.         For $iCC = 1 To UBound($sHold) - 1
  123.             $avArray[$iCC - 1] = $sHold[$iCC]
  124.         Next
  125.         Return $avArray
  126.     Else
  127.         If $vCase = Default Or $vCase = -1 Then
  128.             $vCase = '(?i)'
  129.         Else
  130.             $vCase = ''
  131.         EndIf
  132.         Local $aArray = StringRegExp($sString, '(?s)' & $vCase & $sStart & '(.*?)' & $sEnd, 3)
  133.         If IsArray($aArray) Then Return $aArray
  134.         Return SetError(1, 0, 0)
  135.     EndIf
  136. EndFunc   ;==>_StringBetween
  137.  
  138. ;===============================================================================
  139. ;
  140. ; Function Name:    _StringEncrypt()
  141. ; Description:      RC4 Based string encryption
  142. ; Parameter(s):     $i_Encrypt - 1 to encrypt, 0 to decrypt
  143. ;                   $s_EncryptText - string to encrypt
  144. ;                   $s_EncryptPassword - string to use as an encryption password
  145. ;                   $i_EncryptLevel - integer to use as number of times to encrypt string
  146. ; Requirement(s):   None
  147. ; Return Value(s):  On Success - Returns the string encrypted (blank) times with (blank) password
  148. ;                   On Failure - Returns a blank string and sets @error = 1
  149. ; Author(s):        Wes Wolfe-Wolvereness <Weswolf at aol dot com>
  150. ;
  151. ;===============================================================================
  152. ;
  153. Func _StringEncrypt($i_Encrypt, $s_EncryptText, $s_EncryptPassword, $i_EncryptLevel = 1)
  154.     If $i_Encrypt <> 0 And $i_Encrypt <> 1 Then
  155.         SetError(1)
  156.         Return ''
  157.     ElseIf $s_EncryptText = '' Or $s_EncryptPassword = '' Then
  158.         SetError(1)
  159.         Return ''
  160.     Else
  161.         If Number($i_EncryptLevel) <= 0 Or Int($i_EncryptLevel) <> $i_EncryptLevel Then $i_EncryptLevel = 1
  162.         Local $v_EncryptModified
  163.         Local $i_EncryptCountH
  164.         Local $i_EncryptCountG
  165.         Local $v_EncryptSwap
  166.         Local $av_EncryptBox[256][2]
  167.         Local $i_EncryptCountA
  168.         Local $i_EncryptCountB
  169.         Local $i_EncryptCountC
  170.         Local $i_EncryptCountD
  171.         Local $i_EncryptCountE
  172.         Local $v_EncryptCipher
  173.         Local $v_EncryptCipherBy
  174.         If $i_Encrypt = 1 Then
  175.             For $i_EncryptCountF = 0 To $i_EncryptLevel Step 1
  176.                 $i_EncryptCountG = ''
  177.                 $i_EncryptCountH = ''
  178.                 $v_EncryptModified = ''
  179.                 For $i_EncryptCountG = 1 To StringLen($s_EncryptText)
  180.                     If $i_EncryptCountH = StringLen($s_EncryptPassword) Then
  181.                         $i_EncryptCountH = 1
  182.                     Else
  183.                         $i_EncryptCountH += 1
  184.                     EndIf
  185.                     $v_EncryptModified = $v_EncryptModified & Chr(BitXOR(Asc(StringMid($s_EncryptText, $i_EncryptCountG, 1)), Asc(StringMid($s_EncryptPassword, $i_EncryptCountH, 1)), 255))
  186.                 Next
  187.                 $s_EncryptText = $v_EncryptModified
  188.                 $i_EncryptCountA = ''
  189.                 $i_EncryptCountB = 0
  190.                 $i_EncryptCountC = ''
  191.                 $i_EncryptCountD = ''
  192.                 $i_EncryptCountE = ''
  193.                 $v_EncryptCipherBy = ''
  194.                 $v_EncryptCipher = ''
  195.                 $v_EncryptSwap = ''
  196.                 $av_EncryptBox = ''
  197.                 Local $av_EncryptBox[256][2]
  198.                 For $i_EncryptCountA = 0 To 255
  199.                     $av_EncryptBox[$i_EncryptCountA][1] = Asc(StringMid($s_EncryptPassword, Mod($i_EncryptCountA, StringLen($s_EncryptPassword)) + 1, 1))
  200.                     $av_EncryptBox[$i_EncryptCountA][0] = $i_EncryptCountA
  201.                 Next
  202.                 For $i_EncryptCountA = 0 To 255
  203.                     $i_EncryptCountB = Mod(($i_EncryptCountB + $av_EncryptBox[$i_EncryptCountA][0] + $av_EncryptBox[$i_EncryptCountA][1]), 256)
  204.                     $v_EncryptSwap = $av_EncryptBox[$i_EncryptCountA][0]
  205.                     $av_EncryptBox[$i_EncryptCountA][0] = $av_EncryptBox[$i_EncryptCountB][0]
  206.                     $av_EncryptBox[$i_EncryptCountB][0] = $v_EncryptSwap
  207.                 Next
  208.                 For $i_EncryptCountA = 1 To StringLen($s_EncryptText)
  209.                     $i_EncryptCountC = Mod(($i_EncryptCountC + 1), 256)
  210.                     $i_EncryptCountD = Mod(($i_EncryptCountD + $av_EncryptBox[$i_EncryptCountC][0]), 256)
  211.                     $i_EncryptCountE = $av_EncryptBox[Mod(($av_EncryptBox[$i_EncryptCountC][0] + $av_EncryptBox[$i_EncryptCountD][0]), 256) ][0]
  212.                     $v_EncryptCipherBy = BitXOR(Asc(StringMid($s_EncryptText, $i_EncryptCountA, 1)), $i_EncryptCountE)
  213.                     $v_EncryptCipher &= Hex($v_EncryptCipherBy, 2)
  214.                 Next
  215.                 $s_EncryptText = $v_EncryptCipher
  216.             Next
  217.         Else
  218.             For $i_EncryptCountF = 0 To $i_EncryptLevel Step 1
  219.                 $i_EncryptCountB = 0
  220.                 $i_EncryptCountC = ''
  221.                 $i_EncryptCountD = ''
  222.                 $i_EncryptCountE = ''
  223.                 $v_EncryptCipherBy = ''
  224.                 $v_EncryptCipher = ''
  225.                 $v_EncryptSwap = ''
  226.                 $av_EncryptBox = ''
  227.                 Local $av_EncryptBox[256][2]
  228.                 For $i_EncryptCountA = 0 To 255
  229.                     $av_EncryptBox[$i_EncryptCountA][1] = Asc(StringMid($s_EncryptPassword, Mod($i_EncryptCountA, StringLen($s_EncryptPassword)) + 1, 1))
  230.                     $av_EncryptBox[$i_EncryptCountA][0] = $i_EncryptCountA
  231.                 Next
  232.                 For $i_EncryptCountA = 0 To 255
  233.                     $i_EncryptCountB = Mod(($i_EncryptCountB + $av_EncryptBox[$i_EncryptCountA][0] + $av_EncryptBox[$i_EncryptCountA][1]), 256)
  234.                     $v_EncryptSwap = $av_EncryptBox[$i_EncryptCountA][0]
  235.                     $av_EncryptBox[$i_EncryptCountA][0] = $av_EncryptBox[$i_EncryptCountB][0]
  236.                     $av_EncryptBox[$i_EncryptCountB][0] = $v_EncryptSwap
  237.                 Next
  238.                 For $i_EncryptCountA = 1 To StringLen($s_EncryptText) Step 2
  239.                     $i_EncryptCountC = Mod(($i_EncryptCountC + 1), 256)
  240.                     $i_EncryptCountD = Mod(($i_EncryptCountD + $av_EncryptBox[$i_EncryptCountC][0]), 256)
  241.                     $i_EncryptCountE = $av_EncryptBox[Mod(($av_EncryptBox[$i_EncryptCountC][0] + $av_EncryptBox[$i_EncryptCountD][0]), 256) ][0]
  242.                     $v_EncryptCipherBy = BitXOR(Dec(StringMid($s_EncryptText, $i_EncryptCountA, 2)), $i_EncryptCountE)
  243.                     $v_EncryptCipher = $v_EncryptCipher & Chr($v_EncryptCipherBy)
  244.                 Next
  245.                 $s_EncryptText = $v_EncryptCipher
  246.                 $i_EncryptCountG = ''
  247.                 $i_EncryptCountH = ''
  248.                 $v_EncryptModified = ''
  249.                 For $i_EncryptCountG = 1 To StringLen($s_EncryptText)
  250.                     If $i_EncryptCountH = StringLen($s_EncryptPassword) Then
  251.                         $i_EncryptCountH = 1
  252.                     Else
  253.                         $i_EncryptCountH += 1
  254.                     EndIf
  255.                     $v_EncryptModified &= Chr(BitXOR(Asc(StringMid($s_EncryptText, $i_EncryptCountG, 1)), Asc(StringMid($s_EncryptPassword, $i_EncryptCountH, 1)), 255))
  256.                 Next
  257.                 $s_EncryptText = $v_EncryptModified
  258.             Next
  259.         EndIf
  260.         Return $s_EncryptText
  261.     EndIf
  262. EndFunc   ;==>_StringEncrypt
  263.  
  264. ;===============================================================================
  265. ;
  266. ; Function Name:    _StringInsert(), version 1.02
  267. ; Description:        Inserts a string within another
  268. ; Parameters:         $s_String         - Original string
  269. ;                    $s_InsertString    - String to insert
  270. ;                    $i_Position        - Position to insert string (negatives values
  271. ;                                      count from right hand side)
  272. ; Requirement(s):     None
  273. ; Author(s):          Louis Horvath <celeri at videotron dot ca>
  274. ;
  275. ; Return value:     upon success, returns a string containing the desired insert string.
  276. ;                     upon error, sets @error to the following values:
  277. ;                    @error = 1 : Source string empty / not a string
  278. ;                    @error = 2 : Insert string empty / not a string
  279. ;                    @error = 3 : Invalid position
  280. ;
  281. ;                    and returns original string unmodified.
  282. ;===============================================================================
  283. Func _StringInsert($s_String, $s_InsertString, $i_Position)
  284.     Local $i_Length, $s_Start, $s_End
  285.     
  286.     If $s_String = "" Or (Not IsString($s_String)) Then
  287.         SetError(1) ; Source string empty / not a string
  288.         Return $s_String
  289.     ElseIf $s_InsertString = "" Or (Not IsString($s_String)) Then
  290.         SetError(2) ; Insert string empty / not a string
  291.         Return $s_String
  292.     Else
  293.         $i_Length = StringLen($s_String) ; Take a note of the length of the source string
  294.         If (Abs($i_Position) > $i_Length) Or (Not IsInt($i_Position)) Then
  295.             SetError(3) ; Invalid position
  296.             Return $s_String
  297.         EndIf
  298.     EndIf
  299.     
  300.     ; If $i_Position at start of string
  301.     If $i_Position = 0 Then
  302.         Return $s_InsertString & $s_String ; Just add them up :) Easy :)
  303.         ; If $i_Position is positive
  304.     ElseIf $i_Position > 0 Then
  305.         $s_Start = StringLeft($s_String, $i_Position) ; Chop off first part
  306.         $s_End = StringRight($s_String, $i_Length - $i_Position) ; and the second part
  307.         Return $s_Start & $s_InsertString & $s_End ; Assemble all three pieces together
  308.         ; If $i_Position is negative
  309.     ElseIf $i_Position < 0 Then
  310.         $s_Start = StringLeft($s_String, Abs($i_Length + $i_Position)) ; Chop off first part
  311.         $s_End = StringRight($s_String, Abs($i_Position)) ; and the second part
  312.         Return $s_Start & $s_InsertString & $s_End ; Assemble all three pieces together
  313.     EndIf
  314. EndFunc   ;==>_StringInsert
  315.  
  316. ;===============================================================================
  317. ;
  318. ; Description:      Changes a string to proper case, same a =Proper function in Excel
  319. ; Syntax:           _StringProper( $sString)
  320. ; Parameter(s):     $sString      - String to change to proper case.
  321. ; Requirement(s):   None
  322. ; Return Value(s):  On Success - Returns the proper string.
  323. ;                   On Failure - Returns an empty string and sets @error = 1
  324. ; Author(s):        Jos van der Zande <jdeb at autoitscript dot com>
  325. ; Note(s):          None
  326. ;
  327. ;===============================================================================
  328.  
  329. Func _StringProper($s_Str)
  330.     Local $iX = 0
  331.     Local $CapNext = 1
  332.     Local $s_nStr = ""
  333.     Local $s_CurChar
  334.     For $iX = 1 To StringLen($s_Str)
  335.         $s_CurChar = StringMid($s_Str, $iX, 1)
  336.         Select
  337.             Case $CapNext = 1
  338.                 If __CharacterIsApha($s_CurChar) Then
  339.                     $s_CurChar = StringUpper($s_CurChar)
  340.                     $CapNext = 0
  341.                 EndIf
  342.             Case Not __CharacterIsApha($s_CurChar)
  343.                 $CapNext = 1
  344.             Case Else
  345.                 $s_CurChar = StringLower($s_CurChar)
  346.         EndSelect
  347.         $s_nStr &= $s_CurChar
  348.     Next
  349.     Return ($s_nStr)
  350. EndFunc   ;==>_StringProper
  351.  
  352. ;===============================================================================
  353. ;
  354. ; Description:      Repeats a string a specified number of times.
  355. ; Syntax:           _StringRepeat( $sString, $iRepeatCount )
  356. ; Parameter(s):     $sString      - String to repeat
  357. ;                   $iRepeatCount - Number of times to repeat the string
  358. ; Requirement(s):   None
  359. ; Return Value(s):  On Success - Returns string with specified number of repeats
  360. ;                   On Failure - Returns an empty string and sets @error = 1
  361. ; Author(s):        Jeremy Landes <jlandes at landeserve dot com>
  362. ; Note(s):          None
  363. ;
  364. ;===============================================================================
  365. Func _StringRepeat($sString, $iRepeatCount)
  366.     ;==============================================
  367.     ; Local Constant/Variable Declaration Section
  368.     ;==============================================
  369.     Local $sResult
  370.     
  371.     Select
  372.         Case Not StringIsInt($iRepeatCount)
  373.             SetError(1)
  374.             Return ""
  375.         Case StringLen($sString) < 1
  376.             SetError(1)
  377.             Return ""
  378.         Case $iRepeatCount <= 0
  379.             SetError(1)
  380.             Return ""
  381.         Case Else
  382.             For $iCount = 1 To $iRepeatCount
  383.                 $sResult &= $sString
  384.             Next
  385.             
  386.             Return $sResult
  387.     EndSelect
  388. EndFunc   ;==>_StringRepeat
  389.  
  390. ;===============================================================================
  391. ;
  392. ; Description:      Reverses the contents of the specified string.
  393. ; Syntax:           _StringReverse( $sString )
  394. ; Parameter(s):     $sString - String to reverse
  395. ; Requirement(s):   None
  396. ; Return Value(s):  On Success - Returns reversed string
  397. ;                   On Failure - Returns an empty string and sets @error = 1
  398. ; Author(s):        Jonathan Bennett <jon at hiddensoft com>
  399. ; Note(s):          None
  400. ;
  401. ;===============================================================================
  402. Func _StringReverse($sString)
  403.     ;==============================================
  404.     ; Local Constant/Variable Declaration Section
  405.     ;==============================================
  406.     Local $sReverse
  407.     Local $iCount
  408.     
  409.     If StringLen($sString) >= 1 Then
  410.         For $iCount = 1 To StringLen($sString)
  411.             $sReverse = StringMid($sString, $iCount, 1) & $sReverse
  412.         Next
  413.         
  414.         Return $sReverse
  415.     Else
  416.         SetError(1)
  417.         Return ""
  418.     EndIf
  419. EndFunc   ;==>_StringReverse
  420.  
  421. ;===============================================================================
  422. ;
  423. ; Function Name:    _StringToHex("string")
  424. ; Description:      Convert a string of characters to hexadecimal.
  425. ; Parameter(s):     $strChar is the string you want to convert.
  426. ; Requirement(s):   String Input.
  427. ; Return Value(s):  Returns the converted string in hexadecimal.
  428. ; Author(s):        Jarvis Stubblefield
  429. ; Corrected:        2005/09/04 jpm error checking
  430. ;
  431. ;===============================================================================
  432.  
  433. Func _StringToHex($strChar)
  434.     Local $aryChar, $i, $iDec, $hChar, $strHex
  435.     
  436.     $aryChar = StringSplit($strChar, "")
  437.     
  438.     For $i = 1 To $aryChar[0]
  439.         $iDec = Asc($aryChar[$i])
  440.         $hChar = Hex($iDec, 2)
  441.         $strHex &= $hChar
  442.     Next
  443.     
  444.     Return $strHex
  445.     
  446. EndFunc   ;==>_StringToHex
  447.  
  448. ;=================================================================================
  449. ; Helper functions
  450. Func __CharacterIsApha($s_Str)
  451.     Local $a_Alpha = "abcdefghijklmnopqrstuvwxyz"
  452.     Return (StringInStr($a_Alpha, $s_Str))
  453. EndFunc   ;==>__CharacterIsApha
  454.